package com.birdboot.http;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示HTTP协议中规定的服务器给浏览器回复的响应内容
 * 每个响应由三部分构成
 * 状态行,响应头,响应正文
 */
public class HttpServletResponse {
    private static MimetypesFileTypeMap mftm = new MimetypesFileTypeMap();

    private Socket socket;
    //状态行相关信息
    private int statusCode;//状态代码
    private String statusReason;//状态描述

    //响应头相关信息
    //key:响应头名字  value:响应头对应的值
    private Map<String,String> headers = new HashMap<>();

    //响应正文相关信息
    private File contentFile;//响应正文对应的文件



    public HttpServletResponse(Socket socket){
        this.socket =socket;
    }

    /**
     * 将当前响应对象内容以标准的HTTP响应格式发送给浏览器
     */
    public void response() throws IOException {
        //发送状态行
        sendStatusLine();
        //发送响应头
        sendHeaders();
        //发送响应正文
        sendContent();
    }

    //发送状态行
    private void sendStatusLine() throws IOException {
        println("HTTP/1.1" + " " +statusCode + " " + statusReason);
    }
    //发送响应头
    private void sendHeaders() throws IOException {
        /*
            Map headers
            key             value
            Content-Type    text/html
            Content-Length  12323
            Server          BirdWebServer
         */
        Set<Map.Entry<String,String>> entrySet = headers.entrySet();
        for(Map.Entry<String,String> e : entrySet){
            String key = e.getKey();
            String value = e.getValue();
            println(key + ": " + value);
        }
        //单独发送回车+换行表达响应头发送完毕了
        println("");
    }
    //发送响应正文
    private void sendContent() throws IOException {
        if(contentFile!=null) {
            OutputStream out = socket.getOutputStream();
            FileInputStream fis = new FileInputStream(contentFile);
            byte[] buf = new byte[1024 * 10];
            int len;
            while ((len = fis.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
        }
    }


    /**
     * 向客户端发送一行字符串
     * @param line
     */
    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        out.write(data);
        out.write(13);//发送回车
        out.write(10);//发送换行
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;

        addHeader("Content-Type",mftm.getContentType(contentFile));
        addHeader("Content-Length",contentFile.length()+"");
    }

    /**
     * 添加一个响应头
     * @param name  响应头的名字
     * @param value 响应头的值
     */
    public void addHeader(String name,String value){
        headers.put(name,value);
    }

    /**
     * 要求浏览器重定向到指定路径
     * @param location
     */
    public void sendRedirect(String location){
        //状态代码改为302
        setStatusCode(302);
        setStatusReason("Moved Temporarily");
        //添加响应头Location
        addHeader("Location",location);
    }
}






